package fr.epsi.fragment.pickers;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import java.util.Calendar;

/**
 * Created by Florent Baudon on 22/05/2014.
 */
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    ValidDateSetListener listener;

    public void onAttach(Activity activity){
        super.onAttach(activity);
        try{
            listener = (ValidDateSetListener)activity;
        }catch (NoClassDefFoundError e){
            e.printStackTrace();
        }
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Calendar c = Calendar.getInstance();

        int an = c.get(Calendar.YEAR);
        int mois = c.get(Calendar.MONTH);
        int jour = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, an, mois, jour);
        return dpd;
    }

    @Override
    public void onDateSet(DatePicker datePicker, int an, int mois, int jour) {
        listener.validDateSet(this, an, mois, jour);
    }

    public interface  ValidDateSetListener{
        public void validDateSet(DialogFragment fragment, int an, int mois, int jour);
    }
}